package com.jerry.template.module.mainpage.find.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.template.R;
import com.jerry.template.base.BaseActivity;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mainpage.find.adapter.BannerListAdapter;
import com.jerry.template.module.mainpage.find.entity.BannerList;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;
import com.jerry.template.views.GlideRoundTransform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by chenhuan on 2016/5/14.
 */
public class FindListItemActivity extends BaseActivity{

    @BindView(R.id.name)
    TextView tvName;
    @BindView(R.id.icon)
    ImageView iconImage;
    @BindView(R.id.subscribe_count)
    TextView tvSubscribeCount;
    @BindView(R.id.total_updates)
    TextView tvTotalUpdates;
    @BindView(R.id.intro)
    TextView tvIntro;
    @BindView(R.id.listView)
    PullToRefreshListView listView;


    private int categoryId;
    private String name;
    private String intro;
    private int totalUpdates;
    private int subscribeCount;
    private String iconUrl;

    private List<BannerList.DataBean1.DataBean> dataBeans = new ArrayList<>();
    private BannerListAdapter adapter;



    @Override
    protected int getLayoutId(){
        return R.layout.activity_find_list;
    }
    @OnClick(R.id.back_to_find)
    public void goBack(){
        finish();
    }

    @Override
    protected void initViews(){
        Intent intent = getIntent();

        categoryId = intent.getIntExtra("category_id",100);
        name = intent.getStringExtra("name");
        intro = intent.getStringExtra("intro");
        totalUpdates = intent.getIntExtra("totalUpdates",0);
        subscribeCount = intent.getIntExtra("subscribeCount",0);
        iconUrl = intent.getStringExtra("iconUrl");

        //接收数组
       /* byte [] bis=intent.getByteArrayExtra("iconBitmap");
        Bitmap bitmap= BitmapFactory.decodeByteArray(bis, 0, bis.length);
        iconImage.setImageBitmap(bitmap);*/
        tvName.setText(name);
        tvSubscribeCount.setText(subscribeCount+"");
        tvTotalUpdates.setText(totalUpdates+"");
        tvIntro.setText(intro);
        Glide.with(this).load(iconUrl).transform(new GlideRoundTransform(this,5)).into(iconImage);
    }

    @Override
    protected void loadData(){
        Map<String,Object> params = new HashMap<>();

        params.put("category_id", categoryId);
        OkHttpUtils.postSubmitForm(Urls.DAPEIRIJI_LIST,params);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message){
        if(message != null){
            if(Urls.DAPEIRIJI_LIST.equals(message.type)){
                Gson gson = new Gson();
                BannerList bannerList = gson.fromJson(message.result, BannerList.class);

                dataBeans.addAll(bannerList.getData().getData());

                adapter = new BannerListAdapter(this,dataBeans);

                listView.setFocusable(false);
                listView.setAdapter(adapter);
            }
        }
    }
}
